#include "camera.h"


Camera::Camera()
{
	m_Up= D3DXVECTOR3(0,1,0);
	m_ToEye = D3DXVECTOR3(0,1,-1);
	m_At= D3DXVECTOR3(0,4,0);
	m_Radius=30.0f;
	D3DXQuaternionIdentity(&m_Rotation);


    
}

D3DXMATRIX Camera::getViewMatrix()
{
	D3DXVECTOR4 TmpToEye;
	D3DXVECTOR4 TmpUp;
	D3DXMATRIX  TmpMatrix;
	D3DXMATRIX  View;

	D3DXMatrixRotationQuaternion(&TmpMatrix,&m_Rotation);
	D3DXVec3Transform(&TmpToEye, &m_ToEye, &TmpMatrix);
	D3DXVec3Transform(&TmpUp, &m_Up, &TmpMatrix);

	D3DXVECTOR3 EyePos=m_At+(D3DXVECTOR3(TmpToEye.x,TmpToEye.y,TmpToEye.z)*m_Radius);
	D3DXVECTOR3 Up(TmpUp.x,TmpUp.y,TmpUp.z);



	D3DXMatrixLookAtLH(&View, 
		&EyePos, 
		&m_At, 
		&Up);

	return View;
}

D3DXVECTOR3 Camera::getEyePosition()
{
	D3DXVECTOR3 eyePosition;
	D3DXVECTOR4 tmpToEye;
	D3DXMATRIX  tmpMatrix;
	D3DXMatrixRotationQuaternion(&tmpMatrix,&m_Rotation);
	D3DXVec3Transform(&tmpToEye, &m_ToEye, &tmpMatrix);
	D3DXVECTOR3 toEye=D3DXVECTOR3(tmpToEye.x,tmpToEye.y,tmpToEye.z)*m_Radius;
	D3DXVec3Add(&eyePosition,&m_At,&toEye);
	return eyePosition;
}

void Camera::move(D3DXVECTOR3 v)
{
	D3DXQUATERNION TempRot;
	D3DXMATRIX  TmpMatrix;
	D3DXVECTOR4 TmpVec;

	D3DXMatrixRotationQuaternion(&TmpMatrix,&m_Rotation);
	D3DXVec3Transform(&TmpVec, &v, &TmpMatrix);
	D3DXVECTOR3 vec=D3DXVECTOR3(TmpVec.x,0,TmpVec.z);
	


	m_At+=vec;
}

void Camera::RotateY(float angle)
{
	D3DXQUATERNION TempRot;
	D3DXVECTOR3 Axis(0,1,0);


	D3DXQuaternionRotationAxis(&TempRot,&Axis,angle);
	D3DXQuaternionMultiply(&m_Rotation,&m_Rotation,&TempRot);

}

void Camera::RotateX(float angle)
{
	D3DXQUATERNION TempRot;
	D3DXVECTOR3 Axis(1,0,0);
	D3DXVECTOR4 TmpAxis;
	D3DXMATRIX  TmpMatrix;

	D3DXMatrixRotationQuaternion(&TmpMatrix,&m_Rotation);
	D3DXVec3Transform(&TmpAxis, &Axis, &TmpMatrix);
	Axis=D3DXVECTOR3(TmpAxis.x,TmpAxis.y,TmpAxis.z);

	D3DXQuaternionRotationAxis(&TempRot,&Axis,angle);
	D3DXQuaternionMultiply(&m_Rotation,&m_Rotation,&TempRot);

}

void Camera::Zoom(float step)
{
	m_Radius+=m_Radius*step;
}

D3DXVECTOR3 Camera::GetAtPosition()
{
	return m_At;
}

void Camera::SetAtPosition(D3DXVECTOR3 position)
{
	m_At=position;
}